package pl.koszolko.documentclustering.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pl.koszolko.documentclustering.dao.Authorship;
import pl.koszolko.documentclustering.dao.AuthorshipDao;
import pl.koszolko.documentclustering.dao.utils.DataBaseConnectionManager;

public class JdbcAuthorshipDao implements AuthorshipDao {
	private static final String INSERT = "insert into AUTORZY_PUBLIKACJI VALUES(?,?)";
	private static final String DELETE = "delete from AUTORZY_PUBLIKACJI where ID_PUBLIKACJI = ? and ID_AUTORA = ?";
	private static final String FIND_ALL = "select ID_PUBLIKACJI, ID_AUTORA from AUTORZY_PUBLIKACJI";

	private DataBaseConnectionManager connectionManager;

	public JdbcAuthorshipDao() {
	}
	
	public JdbcAuthorshipDao(DataBaseConnectionManager connectionManager) {
		this.connectionManager = connectionManager;
	}

	@Override
	public void insert(Authorship authorship) {
		Connection conn = null;
		try {
			conn = connectionManager.getConnection();

			PreparedStatement ps = conn.prepareStatement(INSERT);
			ps.setLong(1, authorship.getDocumentId());
			ps.setLong(2, authorship.getAuthorId());
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}

	@Override
	public void delete(Long authorId, Long documentId) {
		Connection conn = null;
		try {
			conn = connectionManager.getConnection();

			PreparedStatement ps = conn.prepareStatement(DELETE);
			ps.setLong(1, documentId);
			ps.setLong(2, authorId);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}

	@Override
	public List<Authorship> findAll() {
		List<Authorship> authorships = new ArrayList<Authorship>();
		Connection conn = null;
		try {
			conn = connectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(FIND_ALL);
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Authorship authorship = new Authorship();
				authorship.setDocumentId(rs.getLong(1));
				authorship.setAuthorId(rs.getLong(2));
				authorships.add(authorship);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}

		return authorships;
	}

	public DataBaseConnectionManager getConnectionManager() {
		return connectionManager;
	}

	public void setConnectionManager(DataBaseConnectionManager connectionManager) {
		this.connectionManager = connectionManager;
	}

}
